<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class File_model extends CI_Model{
		function __contruct(){
			parent::__construct();
			$this->load->library('table');
			$this->load->library('upload');
			$this->load->library('image_lib');
		}
		public function thumbnails($userfile,$id){
			$this->load->library('image_lib');
			$config = array(
				'source_image'      => './uploads/main/'.$userfile, //path to the uploaded image
				'new_image'         => './uploads/resized/', //path to
				'maintain_ratio'    => true,
				'width'             => 150,
				'height'            => 150
				);
				$this->image_lib->initialize($config);
				$this->image_lib->resize();
			$config = array(
				'source_image'      => './uploads/main/'.$userfile,
				'new_image'         => './uploads/thumbs/',
				'maintain_ratio'    => true,
				'width'             => 36,
				'height'            => 36,
				);
				$this->image_lib->initialize($config);
				$this->image_lib->resize();
		}
		public function rethumbnails($userfile,$previd){
	
			$this->load->library('image_lib');
			$config = array(
				'source_image'      => './uploads/main/'.$userfile, //path to the uploaded image
				'new_image'         => './uploads/resized/', //path to
				'maintain_ratio'    => true,
				'width'             => 150,
				'height'            => 150
				);
				$this->image_lib->initialize($config);
				$this->image_lib->resize();
			$config = array(
				'source_image'      => './uploads/main/'.$userfile,
				'new_image'         => './uploads/thumbs/',
				'maintain_ratio'    => true,
				'width'             => 36,
				'height'            => 36,
				);
				$this->image_lib->initialize($config);
				$this->image_lib->resize();
		}
		public function insert2($timedata){
			$this->db->insert('content_table',$timedata);
		return $id = $this->db->insert_id() ; 
		}
		
		public function insert($userfile,$id){	
			
			$this->db->query("INSERT INTO `attachment_table`(`text_id`,`userfile`) VALUES('$id','$userfile')");		
		
		}
		public function reinsert($userfile,$previd){
			$this->db->query("INSERT INTO `attachment_table`(`text_id`,`userfile`) VALUES('$previd','$userfile')");
			
		}
		public function show($id){	

			$query = $this->db->select('*')
						  ->from('content_table')
						  ->join('attachment_table','attachment_table.text_id=content_table.id')
						  ->where('content_table.id',$id)
						  ->get();			  
			return $query;
			
		}
		public function getall(){
	    $query = $this->db->select('*')
						  ->from('content_table')
						  ->join('attachment_table','attachment_table.text_id=content_table.id')
						  ->get();	
			return $query;				
		}

		public function delete_file($id){
			
			$query=$this->db->query("SELECT * FROM attachment_table WHERE id='$id'");
			foreach($query->result() as $row){
			$name = $row->userfile; 
			$extension = explode('.',$name);
			$querry=$this->db->query("DELETE FROM attachment_table WHERE id='$id'");
			$directory1= "./uploads/main";
			unlink($directory1 . "/" . $name);
			//return $extension; 
			if($extension[1] == 'gif'){
				
				$directory2= "./uploads/resized";
				$directory3= "./uploads/thumbs";
				unlink($directory2 . "/" . $name);
				unlink($directory3 . "/" . $name);
				}
			elseif($extension[1] == 'jpeg'){
				
				$directory2= "./uploads/resized";
				$directory3= "./uploads/thumbs";
				unlink($directory2 . "/" . $name);
				unlink($directory3 . "/" . $name);
				}
			elseif($extension[1] == 'jpg'){
				
				$directory2= "./uploads/resized";
				$directory3= "./uploads/thumbs";
				unlink($directory2 . "/" . $name);
				unlink($directory3 . "/" . $name);
				}
			elseif($extension[1] == 'png'){
				
				$directory2= "./uploads/resized";
				$directory3= "./uploads/thumbs";
				
				unlink($directory2 . "/" . $name);
				unlink($directory3 . "/" . $name);
				}			
			
			}
			$msg="your file successfully Deleted";
			return $msg;

		}

		function delete_checked($checked_files) {
			//$checked_files = array();
			//print_r($checked_files);exit();
			foreach ($checked_files as $file_id){
			  $this->db->select('id');
			  $this->db->from('attachment_table');
			  $this->db->where('id', $file_id);
			  $this->db->limit(1);
			  $query = $this->db->get();
			  if ($query->num_rows() > 0) {  	
				$querrry=$this->db->query("SELECT * FROM attachment_table WHERE id='$file_id'");
				foreach($querrry->result() as $row){
			$name = $row->userfile; 
			$extension = explode('.',$name);
			$querry=$this->db->query("DELETE FROM attachment_table WHERE id='$file_id'");
			$directory1= "./uploads/main";
			unlink($directory1 . "/" . $name);
			//return $extension; 
			if($extension[1] == 'gif'){
				
				$directory2= "./uploads/resized";
				$directory3= "./uploads/thumbs";
				unlink($directory2 . "/" . $name);
				unlink($directory3 . "/" . $name);
				}
			elseif($extension[1] == 'jpeg'){
				
				$directory2= "./uploads/resized";
				$directory3= "./uploads/thumbs";
				unlink($directory2 . "/" . $name);
				unlink($directory3 . "/" . $name);
				}
			elseif($extension[1] == 'jpg'){
				
				$directory2= "./uploads/resized";
				$directory3= "./uploads/thumbs";
				unlink($directory2 . "/" . $name);
				unlink($directory3 . "/" . $name);
				}
			elseif($extension[1] == 'png'){
				
				$directory2= "./uploads/resized";
				$directory3= "./uploads/thumbs";
				
				unlink($directory2 . "/" . $name);
				unlink($directory3 . "/" . $name);
				}
			}
		}
			  else {
					return FALSE;
			  } 
			}
				echo"your file successfully Deleted";
		}
		function download_checked($checked_files){
			
			foreach($checked_files as $row){
				//$filename[]= $row;
				$this->db->select('userfile');
				$this->db->from('attachment_table');
				$this->db->where('id',$row);
				$query =$this->db->get();
				foreach($query->result() as $row2){
					$filename[]=$row2->userfile;
				}
			}
			return $filename;
		  }
		  
		 } 
//$this->zip->download('files_backup.zip');
?>


